﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BikeSystem.Models;
using BikeSystem.BLL;

namespace BikeUI
{
    public partial class FormBills : Form
    {

        RentalManager rentalManager = new RentalManager();
        public FormBills()
        {
            InitializeComponent();
        }

        private void FormBills_Activated(object sender, EventArgs e)
        {
            loadBills();
        }

        private void txtFullText_TextChanged(object sender, EventArgs e)
        {

            loadBills();
        }


        private void billDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            rentalBindingSource.Position = billBindingSource.Position;
        }


        private void loadBills()
        {
            IList<Rental> filtedRentals = new List<Rental>();
            IList<Rental> rentals = rentalManager.GetAllRentals();
            string fullTxt = txtFullText.Text;
            if (string.IsNullOrEmpty(fullTxt))
            {
                filtedRentals = rentals;
            }
            else
            {
                fullTxt = fullTxt.Trim().ToUpper();

                filtedRentals = rentals.Where(p => ((!string.IsNullOrEmpty(p.BikeNo) && p.BikeNo.ToUpper().Contains(fullTxt)) ||
                                                                     (!string.IsNullOrEmpty(p.RentNo) && p.RentNo.ToUpper().Contains(fullTxt)) ||
                                                                     (!string.IsNullOrEmpty(p.RenterName) && p.RenterName.ToUpper().Contains(fullTxt)) ||
                                                                     (!string.IsNullOrEmpty(p.OperatorName) && p.OperatorName.ToUpper().Contains(fullTxt)) ||
                                                                     (null != p.Bill && !string.IsNullOrEmpty(p.Bill.BillNo) && p.Bill.BillNo.ToUpper().Contains(fullTxt)) ||
                                                                     (null != p.Bill && !string.IsNullOrEmpty(p.Bill.PaymentMethod) && p.Bill.PaymentMethod.ToUpper().Contains(fullTxt)))).ToList<Rental>();
            }
            billBindingSource.Clear();

            if(filtedRentals.Count > 0){
                foreach(Rental rental in filtedRentals){
                    billBindingSource.Add(rental.Bill);
                }
            }
            rentalBindingSource.DataSource = filtedRentals;
        
        }


    }
}
